<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Test an analyzer | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'test-analyzer.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/test-analyzer.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/test-analyzer.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/test-analyzer.html" rel="nofollow" target="_blank">../en/test-analyzer.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="analysis.html">Text analysis</a></span>
»
<span class="breadcrumb-link"><a href="configure-text-analysis.html">Configure text analysis</a></span>
»
<span class="breadcrumb-node">Test an analyzer</span>
</div>
<div class="navheader">
<span class="prev">
<a href="configure-text-analysis.html">« Configure text analysis</a>
</span>
<span class="next">
<a href="configuring-analyzers.html">Configuring built-in analyzers »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="test-analyzer"></a>Test an analyzer<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/testing.asciidoc">edit</a>
</h2>
</div></div></div>
<p>The <a class="xref" href="indices-analyze.html" title="Analyze API"><code class="literal">analyze</code> API</a> is an invaluable tool for viewing the
terms produced by an analyzer. A built-in analyzer can be specified inline in
the request:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST _analyze
{
  "analyzer": "whitespace",
  "text":     "The quick brown fox."
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/796.console"></div>
<p>The API returns the following response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "tokens": [
    {
      "token": "The",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 0
    },
    {
      "token": "quick",
      "start_offset": 4,
      "end_offset": 9,
      "type": "word",
      "position": 1
    },
    {
      "token": "brown",
      "start_offset": 10,
      "end_offset": 15,
      "type": "word",
      "position": 2
    },
    {
      "token": "fox.",
      "start_offset": 16,
      "end_offset": 20,
      "type": "word",
      "position": 3
    }
  ]
}</pre>
</div>
<p>You can also test combinations of:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
A tokenizer
</li>
<li class="listitem">
Zero or more token filters
</li>
<li class="listitem">
Zero or more character filters
</li>
</ul>
</div>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST _analyze
{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding" ],
  "text":      "Is this déja vu?"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/797.console"></div>
<p>The API returns the following response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "tokens": [
    {
      "token": "is",
      "start_offset": 0,
      "end_offset": 2,
      "type": "&lt;ALPHANUM&gt;",
      "position": 0
    },
    {
      "token": "this",
      "start_offset": 3,
      "end_offset": 7,
      "type": "&lt;ALPHANUM&gt;",
      "position": 1
    },
    {
      "token": "deja",
      "start_offset": 8,
      "end_offset": 12,
      "type": "&lt;ALPHANUM&gt;",
      "position": 2
    },
    {
      "token": "vu",
      "start_offset": 13,
      "end_offset": 15,
      "type": "&lt;ALPHANUM&gt;",
      "position": 3
    }
  ]
}</pre>
</div>
<div class="sidebar">
<div class="titlepage"><div><div>
<p class="title"><strong>Positions and character offsets</strong></p>
</div></div></div>
<p>As can be seen from the output of the <code class="literal">analyze</code> API, analyzers not only
convert words into terms, they also record the order or relative <em>positions</em>
of each term (used for phrase queries or word proximity queries), and the
start and end <em>character offsets</em> of each term in the original text (used for
highlighting search snippets).</p>
</div>
<p>Alternatively, a <a class="xref" href="analysis-custom-analyzer.html" title="Create a custom analyzer"><code class="literal">custom</code> analyzer</a> can be
referred to when running the <code class="literal">analyze</code> API on a specific index:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "std_folded": { <a id="CO369-1"></a><i class="conum" data-value="1"></i>
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "my_text": {
        "type": "text",
        "analyzer": "std_folded" <a id="CO369-2"></a><i class="conum" data-value="2"></i>
      }
    }
  }
}

GET my_index/_analyze <a id="CO369-3"></a><i class="conum" data-value="3"></i>
{
  "analyzer": "std_folded", <a id="CO369-4"></a><i class="conum" data-value="4"></i>
  "text":     "Is this déjà vu?"
}

GET my_index/_analyze <a id="CO369-5"></a><i class="conum" data-value="3"></i>
{
  "field": "my_text", <a id="CO369-6"></a><i class="conum" data-value="5"></i>
  "text":  "Is this déjà vu?"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/798.console"></div>
<p>The API returns the following response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "tokens": [
    {
      "token": "is",
      "start_offset": 0,
      "end_offset": 2,
      "type": "&lt;ALPHANUM&gt;",
      "position": 0
    },
    {
      "token": "this",
      "start_offset": 3,
      "end_offset": 7,
      "type": "&lt;ALPHANUM&gt;",
      "position": 1
    },
    {
      "token": "deja",
      "start_offset": 8,
      "end_offset": 12,
      "type": "&lt;ALPHANUM&gt;",
      "position": 2
    },
    {
      "token": "vu",
      "start_offset": 13,
      "end_offset": 15,
      "type": "&lt;ALPHANUM&gt;",
      "position": 3
    }
  ]
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO369-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Define a <code class="literal">custom</code> analyzer called <code class="literal">std_folded</code>.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO369-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>The field <code class="literal">my_text</code> uses the <code class="literal">std_folded</code> analyzer.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO369-3"><i class="conum" data-value="3"></i></a><a href="#CO369-5"></a></p>
</td>
<td align="left" valign="top">
<p>To refer to this analyzer, the <code class="literal">analyze</code> API must specify the index name.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO369-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>Refer to the analyzer by name.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO369-6"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>Refer to the analyzer used by field <code class="literal">my_text</code>.</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="configure-text-analysis.html">« Configure text analysis</a>
</span>
<span class="next">
<a href="configuring-analyzers.html">Configuring built-in analyzers »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>